package com.shimao.celnet.payment.model.response;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

/**
 * 接口返回基础类
 * Created by ensure.xu on 2017/11/24.
 */
public class ResponseJson {
    private JSONObject body = new JSONObject();

    /**
     * 响应代码
     */
    private String errorCode;

    /**
     * 响应消息
     */
    private String errorMessage;



    public ResponseJson() {
        this.errorCode = "";
        this.errorMessage = "";
    }

    /**
     * 接口返回信息设定
     * @param errorInfo
     */
    public ResponseJson(ErrorInfoInterface errorInfo) {

        this.errorCode = errorInfo.getErrorCode();

        this.errorMessage = errorInfo.getErrorMessage();

    }

    /**
     * 接口返回信息设定
     * @param errorInfo
     * @param strField 错误信息参数
     */
    public ResponseJson(ErrorInfoInterface errorInfo,String strField) {

        this.errorCode = errorInfo.getErrorCode();

        if(StringUtils.isNotEmpty(strField)){
            this.errorMessage = String.format(errorInfo.getErrorMessage(),strField);
        }else{
            this.errorMessage = errorInfo.getErrorMessage();
        }
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public void addBody(String key, JSONArray value) {
        body.put(key, value);
    }

    public void addBody(String key, JSONObject value) {
        body.put(key, value);
    }

    public void addBody(String key, String value) {
        body.put(key, value);
    }

    public String toString() {
        JSONObject j = new JSONObject();
        j.put("errcode", errorCode);
        j.put("errmsg", errorMessage);
        j.put("data", body);
        return j.toString();
    }
}
